<template>
  <div class="mod-card__dealer">
    <div class="top-search">
      <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()" label-position="top">
        <div class="top-search_form">
          <div class="ttop-search_form_input">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item :label="$t('deviceAuth.name')">
                  <el-input v-model="state.dataForm.name" :placeholder="$t('input')" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item :label="$t('deviceAuth.personType')">
                  <el-select v-model="state.dataForm.personType" clearable>
                    <el-option v-for="k in Object.keys(PersonTypeMap)" :key="k" :label="PersonTypeMap[k]" :value="k" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item :label="$t('deviceAuth.gender')">
                  <el-select v-model="state.dataForm.gender" clearable>
                    <el-option :key="GenderEnum.MAN" label="男" :value="GenderEnum.MAN" />
                    <el-option :key="GenderEnum.WOMAN" label="女" :value="GenderEnum.WOMAN" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item :label="$t('deviceAuth.status')" clearable>
                  <el-select v-model="state.dataForm.status">
                    <el-option :key="1" label="成功" :value="1" />
                    <el-option :key="-1" label="失败" :value="-1" />
                  </el-select>
                </el-form-item>
              </el-col></el-row>
          </div>
          <div class="top-search_form_btn">
            <el-form-item>
              <el-form-item>
                <el-button @click="state.getDataList()">{{ $t("reset") }}</el-button>
              </el-form-item>
              <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="addOrUpdateHandle()">批量下发</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="selfCheckHandle()">设备数据自检</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>

    <div class="content">
      <el-row>
        <el-col :span="4">
          <el-tree :data="deptList" :props="defaultProps" default-expand-all highlight-current ref="treeRef"
            @node-click="nodeCilck"></el-tree>
        </el-col>
        <el-col :span="1" style="display: flex;justify-content: center;">
          <el-divider style="height: calc(100vh - 300px)" direction="vertical"></el-divider>
        </el-col>
        <el-col :span="19">

          <el-table v-loading="state.dataListLoading" :data="state.dataList" border
            @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
            <el-table-column prop="name" :label="$t('deviceAuth.name')" header-align="center"
              align="center"></el-table-column>
            <el-table-column prop="gender" :label="$t('deviceAuth.gender')" header-align="center" align="center"
              width="120">
              <template v-slot="scope">
                {{ GenderMap[scope.row.gender] }}
              </template>
            </el-table-column>
            <el-table-column prop="personType" :label="$t('deviceAuth.personType')" header-align="center" align="center"
              width="120">
              <template v-slot="scope">
                {{ PersonTypeMap[scope.row.personType] || '学生' }}
              </template>
            </el-table-column>
            <el-table-column prop="faceId" :label="$t('deviceAuth.faceId')" header-align="center"
              align="center"></el-table-column>
            <el-table-column prop="classId" :label="$t('deviceAuth.classId')" header-align="center"
              align="center"></el-table-column>
            <el-table-column prop="device" :label="$t('deviceAuth.device')" header-align="center" align="center"
              width="150">
              <template v-slot="scope">
                <!-- <span :style="{ color: (scope.row?.deviceAuthInfoList).filter(k => k.status === 1).length === scope.row?.deviceAuthInfoList.length ? 'green' : 'red' }">
            {{ scope.row?.deviceAuthInfoList.filter(k => k.status === 1).length }} / {{ scope.row?.deviceAuthInfoList.length }}
          </span> -->
                <el-popover placement="right" :width="400" trigger="click">
                  <template #reference>
                    <el-button type="primary" text>设备下发详情</el-button>
                  </template>
                  <el-table :data="scope.row?.deviceAuthInfoList">
                    <el-table-column property="deviceCode" label="设备编码" />
                    <el-table-column property="status" label="下发状态">
                      <template v-slot="scope">
                        <el-tag type="success" v-if="scope.row.status === 1">成功</el-tag>
                        <el-tag type="danger" v-else>失败</el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column property="status" :label="$t('handle')">
                      <template v-slot="scope2">
                        <el-button type="primary" link @click="deleteHandle(scope.row.faceId, scope2.row.deviceCode)">{{
        $t("delete") }}</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-popover>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
            :total="state.total" layout="total, sizes, prev, pager, next, jumper"
            @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
        </el-col>
      </el-row>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { onMounted, reactive, ref, toRefs } from "vue";
import AddOrUpdate from "./issue.vue";
import { GenderEnum, GenderMap } from './contant';
import { useI18n } from "vue-i18n";
import { ElMessage, ElMessageBox } from "element-plus";
import { PersonTypeMap } from "@/constants/config";

const { t } = useI18n();
const defaultProps = reactive({
  children: 'children',
  label: 'name'
})

const view = reactive({
  getDataListURL: "device/auth/queryList",
  getDataListIsPage: true,
  deleteURL: "/device/auth/remove",
  deleteIsBatch: true,
  deleteIsPost: true,
  dataForm: {
    name: "",
    classId: "",
    gender: null,
    status: null,
    personType: null,
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

// 获取部门列表
const deptList = ref<any[]>([]);
const getDeptList = () => {
  return baseService.get("/data/class/list").then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    deptList.value = res.data;
  });
};

const treeRef = ref();
const nodeCilck = () => {
  const node = treeRef.value.getCurrentNode();
  state.dataForm.classId = node.id
  state.getDataList()
}

const selfCheckHandle = () => {
  baseService.post("/device/auth/selfCheck").then((res) => {
    ElMessage.success({
      message: t("prompt.success"),
      duration: 500,
      onClose: () => {
        state.getDataList();
      }
    });
  });
};

const deleteHandle = (faceId: string, deviceCode: string) => {
  ElMessageBox.confirm(t("prompt.info", { handle: t("delete") }), t("prompt.title"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning"
  })
    .then(() => {
      baseService.post("/device/auth/remove", {
        deviceCode: deviceCode,
        faceId: faceId,
      }).then((res) => {
        ElMessage.success({
          message: t("prompt.success"),
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      });
    });
};

onMounted(() => {
  getDeptList();
});
</script>
